package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> findAll() {
        List<Role> list = roleMapper.findAll();
        return list;
    }

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        List<Role> list = roleMapper.findByCondition(queryString);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增角色
     *
     * @param permissionIds
     * @param role
     */
    @Override
    public void add(Integer[] permissionIds, Role role) {
        //新增角色
        roleMapper.add(role);
        //设置角色和权限的中间表
        setRoleAndPermission(permissionIds, role.getId());
    }

    //新增角色和权限的关系
    private void setRoleAndPermission(Integer[] permissionIds, Integer id) {
        //设置检查项和检查组的中间表
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                //循环中间表关系
                roleMapper.setRoleAndPermission(permissionId, id);
            }
        }
    }

    /**
     * 根据id删除角色
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        //通过角色的id查询是否在中间表中被引用
        Integer count = roleMapper.findCountByRoleId(id);
        if (count > 0) {
            //抛出当前角色被引用，不能删除
            throw new RuntimeException(MessageConstant.ROLE_IS_QUOTED);
        }

        //删除中间表关系
        roleMapper.deleteAssociation(id);
        roleMapper.deleteById(id);
    }

    /**
     * 根据id查询需要编辑的角色
     *
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        //通过角色的id查询是否在中间表中被引用
        Integer count = roleMapper.findCountByRoleId(id);
        if (count > 0) {
            //抛出当前角色被引用，不能编辑
            throw new RuntimeException(MessageConstant.EDIT_ROLE_IS_QUOTED);
        }
        Role role = roleMapper.findById(id);
        return role;
    }

    /**
     * 更新被编辑的角色
     * @param permissionIds
     * @param role
     */
    @Override
    public void edit(Integer[] permissionIds, Role role) {
        //删除中间表关系
        roleMapper.deleteAssociation(role.getId());
        //重新设置中间表关系
        setRoleAndPermission(permissionIds, role.getId());
        //更新checkgroup
        roleMapper.edit(role);

    }

}
